﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Runda.CMS.Common.Entity;
using System.Collections.Generic;
using Runda.CMS.Bll;

namespace Runda.CMS.Portal.NoticeManage
{
    public partial class DutyGroupAdd : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }

        public bool IsEdit
        {
            get {
                return Convert.ToBoolean(ViewState["ISEDIT"].ToString());
            }
            set {
                ViewState["ISEDIT"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["DutyGroupID"] != null)
                {
                    txtDutyGroupID.Value = Request.QueryString["DutyGroupID"];
                    IsEdit = true;
                    SystemBll bll = new SystemBll();
                    SysDutyGroupEntity entity = null;
                    entity = bll.Get<SysDutyGroupEntity>(" and DutyGroupID = '" + txtDutyGroupID.Value + "'");;
                    if (entity != null)
                    {
                        txtDutyGroupName.Text = entity.DuctyGroupName;
                    }
                }
                else
                {
                    txtDutyGroupID.Value = Guid.NewGuid().ToString();
                    IsEdit = false;
                }
                BindGrid();
            }
        }

        protected void lnkbtnSave_Click(object sender, EventArgs e)
        {
            Save();
            IsEdit = true;
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功!');window.opener.document.getElementById('ctl00_cphBody_btnRefersh').click();", true);
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            Save();
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功!');window.opener.document.getElementById('ctl00_cphBody_btnRefersh').click();window.close();", true);
        }

        private void Save()
        {
            SystemBll bll = new SystemBll();
            SysDutyGroupEntity entity = null;
            if (!IsEdit)
            {
                entity = new SysDutyGroupEntity(new Guid(txtDutyGroupID.Value));
                entity.CreatedBy = base.CurrentUserID;
                entity.CreatedOn = DateTime.Now;
            }
            else
            {
                entity = bll.Get<SysDutyGroupEntity>(" and DutyGroupID = '" + txtDutyGroupID .Value+ "'");

            }

            entity.DuctyGroupName = txtDutyGroupName.Text;

            bll.InsertOrUpdate(entity);
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void gridContractItems_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                SystemBll bll = new SystemBll();
                bll.Delete<SysDutyGroupUserEntity>(" and DutyGroupUserID='"+e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["DutyGroupUserID"].ToString()+"'");
            }
            BindGrid();
        }

        private void BindGrid()
        { 
            SystemBll bll = new SystemBll();
            List<SysDutyGroupUserEntity> list = bll.GetAll<SysDutyGroupUserEntity>(" and DutyGroupID = '" + txtDutyGroupID .Value+ "'");

            gridUsers.DataSource = list;
            gridUsers.DataBind();
        }
    }
}
